package com.android.meige.fragment.group;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.meige.R;
import com.android.meige.fragment.BaseLazyFragment;
import com.android.meige.fragment.detail.Fragment_SALC_TabList;
import com.android.meige.model.group.SeaAmoyListData;
import com.android.meige.model.group.SeaAmoyListDataCallback;
import com.android.meige.utils.ConstantsUrl;
import com.android.meige.utils.HttpUtils;
import com.android.meige.utils.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/7/11.
 */

public class Fragment_SeaAmoyLClIST extends BaseLazyFragment {

    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.sort_tab)
    TabLayout tabLayout;
    @BindView(R.id.pager)
    ViewPager sortViewpager;
    @BindView(R.id.gridView_sort)
    GridView gridView_sort;
    @BindView(R.id.pop_sort)
    LinearLayout popSort;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private LayoutInflater inflater;
    private View view;
    private FragmentActivity mcontext;
    private boolean networkConnected;
    private GroupFragmentPagerAdapters pagerAdapter;
    private StaggeredGridLayoutManager mLayoutManager;
    private ArrayList<SeaAmoyListData.DataBean.Cat_listBean> cat_list = new ArrayList<>();
    private String nid;
    private int differ;
    private int lastPosition;

    public static Fragment_SeaAmoyLClIST newInstance(int differ, String nid) {
        Fragment_SeaAmoyLClIST fragment = new Fragment_SeaAmoyLClIST();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, nid);
        args.putInt(ARG_PARAM2, differ);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            nid = getArguments().getString(ARG_PARAM1);
            differ = getArguments().getInt(ARG_PARAM2);
        }
    }

    @OnClick({R.id.click_resetnetwork_refresh, R.id.close_img, R.id.close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.close_img:
                popSort.setVisibility(View.GONE);
                break;
            case R.id.close:
                popSort.setVisibility(View.GONE);
                break;
        }
    }

    //    private class GridSearchSortAdapt extends BaseAdapter {
//
//
//        private ArrayList<SeaAmoyListData.DataBean.Cat_listBean> listdata;
//
//
//        public GridSearchSortAdapt(ArrayList<SeaAmoyListData.DataBean.Cat_listBean> listdata) {
//            this.listdata = listdata;
//        }
//
//        public void setSeclection(int position) {
//            lastPosition = position;
//        }
//
//        @Override
//        public int getCount() {
//            return listdata.size();
//        }
//
//        @Override
//        public BaseObject getItem(int position) {
//            return null;
//        }
//
//        @Override
//        public long getItemId(int position) {
//            return 0;
//        }
//
//        @Override
//        public View getView(int position, View convertView, ViewGroup parent) {
//
//            View view = mcontext.getLayoutInflater().inflate(R.layout.item_main_type_sort, null);
//            TextView tv_sort = (TextView) view.findViewById(R.id.tv_sort);
//            tv_sort.setText(listdata.get(position).getCatname());
//            if (lastPosition == position) {
//                tv_sort.setTextColor(Color.parseColor("#FF2741"));
//            } else {
//                tv_sort.setTextColor(Color.parseColor("#5a5a5a"));
//            }
//
//            return view;
//        }
//
//    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_seaamoylcmain, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        GetSeaAmoyLCMain();
    }

    private void initUI() {

    }

    private class GroupFragmentPagerAdapters extends FragmentPagerAdapter {

        public GroupFragmentPagerAdapters(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return Fragment_SALC_TabList.newInstance(cat_list.get(position).getId());
        }

        @Override
        public int getCount() {
            return cat_list.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return cat_list.get(position).getCatname();
        }
    }

    private void GetSeaAmoyLCMain() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.NATIONGOODSLIST;
            HashMap<String, String> map = new HashMap<>();
            map.put("id", nid);
            map.put("top", nid);

            HttpUtils.get(address, map, new SeaAmoyListDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(SeaAmoyListData response, int id) {
                    if (response == null) {
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0 && response.getData() != null) {
                        SeaAmoyListData.DataBean data = response.getData();
                        List<SeaAmoyListData.DataBean.Cat_listBean> catList = data.getCat_list();
                        if (ObjectUtils.listHasData(catList)) {
                            cat_list.clear();
                            cat_list.addAll(catList);
                            pagerAdapter = new GroupFragmentPagerAdapters(mcontext.getSupportFragmentManager());
                            sortViewpager.setAdapter(pagerAdapter);
                            sortViewpager.setCurrentItem(differ);
                            tabLayout.setupWithViewPager(sortViewpager);
                        }
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                    }
                }
            });
        } else {
            ObjectUtils.toast(mcontext, "网络未连接");
        }
    }
}
